AI-Agent 工具探索

更新至 20260517

资源

正文

Deepseek API

先从 DeepSeek Platform 处买一点 Token。

webp

整一个 OpenAI 形式的 API Key(以 sk- 开头):

webp

参考 API 文档(Your First API Call | DeepSeek API Docs)目前可提供的模型有:

PARAMVALUE
base_url (OpenAI)https://api.deepseek.com
base_url (Anthropic)https://api.deepseek.com/anthropic
api_keyapply for an API key
model*deepseek-v4-flash deepseek-v4-pro deepseek-chat (to be deprecated on 2026/07/24) deepseek-reasoner (to be deprecated on 2026/07/24)

Python 调用

python
from openai import OpenAI
 
client = OpenAI(
    api_key="sk-xxxxxxxx",
    base_url="https://api.deepseek.com"
)
 
response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {
            "role": "user",
            "content": "河北大学在哪里?"
        }
    ]
)
 
print(response.choices[0].message.content)
markdown
河北大学位于**河北省保定市**。具体有两个主要校区:
 
1.  **五四路校区(校本部/老校区)**:地址为保定市莲池区五四东路180号。
2.  **七一路校区(新校区)**:地址为保定市莲池区七一东路2666号。
 
此外,学校还有裕华路校区(医学部)。河北大学的办学主体和主要教学科研活动主要集中在五四路校区和七一路校区。

Powershell 环境变量

在 Powershell 中将 API Key 记录。

如此设置只对当前会话有效:

shell
$env:OPENAI_API_KEY="sk-xxxx"

系统环境变量设置,这将持续有效:

shell
setx OPENAI_API_KEY "sk-xxxx"

使用 api_key=os.getenv("OPENAI_API_KEY") 获取对应环境变量的 API Key。即可放置将 API Key 泄露在代码中:

webp

简单的 Agent

如果在目前的环境直接问“现在几点?”将得不到有效答复:

markdown
抱歉,我无法直接获取当前的实时时间。如果你需要知道现在的具体时间,可以查看设备上的时钟显示(比如手机、电脑或手表)。如果有其他问题,欢迎随时问我! 😊
webp

让 LLM 能够调用代码里的函数:

python
import os
import json
from openai import OpenAI
from datetime import datetime
 
# =========================
# 1. Client(DeepSeek / OpenAI)
# =========================
client = OpenAI(
    api_key=os.getenv("OPENAI_API_KEY"),
    base_url="https://api.deepseek.com"
)
 
# =========================
# 2. Tools
# =========================
 
def get_time():
    return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
 
 
def calculator(expression: str):
    try:
        return str(eval(expression))
    except Exception as e:
        return f"error: {str(e)}"
 
 
TOOLS = {
    "get_time": get_time,
    "calculator": calculator,
}
 
# =========================
# 3. System Prompt
# =========================
 
SYSTEM_PROMPT = """
你是一个工具调用型 Agent。
 
你必须严格输出 JSON,不能输出任何多余文本。
 
输出格式只有两种:
 
1. 调用工具:
{
  "action": "tool",
  "name": "tool_name",
  "args": {}
}
 
2. 最终回答:
{
  "action": "final",
  "answer": "xxx"
}
 
规则:
- 时间问题必须用 get_time
- 计算必须用 calculator
- 不允许编造工具结果
"""
 
# =========================
# 4. Agent Loop
# =========================
 
def run_agent(user_input: str):
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": user_input}
    ]
 
    while True:
        resp = client.chat.completions.create(
            model="deepseek-chat",
            messages=messages,
            response_format={"type": "json_object"}
        )
 
        content = resp.choices[0].message.content
        print("\n[LLM raw output]", content)
 
        # -------------------------
        # parse JSON safely
        # -------------------------
        try:
            data = json.loads(content)
        except Exception:
            return "LLM output is not valid JSON"
 
        # =========================
        # TOOL CALL
        # =========================
        if data["action"] == "tool":
            tool_name = data["name"]
            args = data.get("args", {})
 
            print(f"[Tool call] {tool_name} {args}")
 
            tool_result = TOOLS[tool_name](**args)
 
            messages.append({
                "role": "assistant",
                "content": content
            })
 
            messages.append({
                "role": "user",
                "content": f"tool_result: {tool_result}"
            })
 
        # =========================
        # FINAL ANSWER
        # =========================
        else:
            return data["answer"]
 
# =========================
# 5. Run
# =========================
 
if __name__ == "__main__":
    while True:
        q = input("\nYou: ")
        if q.lower() in ["exit", "quit"]:
            break
 
        ans = run_agent(q)
        print("\nAgent:", ans)
markdown
You: What is the time now?
 
[LLM raw output] {"action": "tool", "name": "get_time", "args": {}}
[Tool call] get_time {}
 
[LLM raw output] {"action": "final", "answer": "The current time is 2026-05-17 10:25:08."}
 
Agent: The current time is 2026-05-17 10:25:08.

Codex App

Codex DS — DeepSeek V4 接入 Codex Desktop 下个中转器,然后安装 Codex - Windows官方下载 | 微软应用商店 | Microsoft Store。即可拿自己的 DeepSeek API 使用 Codex。

webp

Cherry Studio

填好 API Key 和 Model 直接用:

webp

OpenClaw

这个好像租一个服务器,然后在服务器上搭比较合适。

Windows 下(管理员的 Powershell):

shell
npm i -g openclaw
openclaw onboard

设置好,服务器将被启动:

webp

启动客户端:

shell
openclaw dashboard
webp

接入微信

OpenCode

安装:

shell
npm install -g opencode-ai

启动:

shell
opencode

不需要 API Key 就能免费耍一耍:

webp

命令 \connect 用自己的 API Key:

webp

Cline

VSC 上装这个插件。配置好 API Key。

webp

像 Cursor、Copilot 一样问问题:

webp

Hermes

好像跟 OpenClaw 差不多。

维度OpenClawHermes
核心结构Gateway 控制中心Agent 自循环学习
任务组织Skills 插件系统自动生成技能
控制方式人类主导配置AI 自主优化
多渠道支持很强(聊天平台整合)较弱
记忆系统外部化/结构化分层+经验记忆
学习能力靠人维护 skills自动进化